<template>
    <div>
        <div class="bg-banner" />
        <div id="login-box">
            <div class="login-banner" />
            <el-form v-if="type=='pwd' || type=='phone'" ref="form" :model="form" :rules="rules" class="login-form" auto-complete="on" label-position="left">
                <div v-if="type=='pwd'">
                    <div class="title-container">
                        <h3 class="title">{{ title }}</h3>
                    </div>
                    <el-form-item prop="phone">
                        <el-input v-model="form.phone" placeholder="用户名" type="text" />
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input v-model="form.password" :type="passwordType" placeholder="密码" @keyup.enter.native="handleLogin">
                            <svg-icon slot="suffix" :name="passwordType === 'password' ? 'eye' : 'eye-open'" @click="showPassword" />
                        </el-input>
                    </el-form-item>
                    <el-button v-loading="loading" type="primary" style="width: 100%;" @click.native.prevent="handleLogin">登 录</el-button>
                    <div class="type-check" @click="typeCheck">{{ type=='pwd'?'手机验证码登录':'账号密码登录' }}</div>
                </div>
                <div v-if="type=='phone'">
                    <div class="title-container">
                        <h3 class="title">{{ title }}</h3>
                    </div>
                    <el-form-item prop="phone">
                        <el-input v-model="form.phone" placeholder="手机号码" type="text" />
                    </el-form-item>
                    <el-form-item prop="code" class="get-code">
                        <el-input v-model="form.code" placeholder="验证码" @keyup.enter.native="handleLogin">
                            <template slot="append"><el-button type="text" :disabled="btnDisabled" style="width: 100px;" @click="getCode">{{ btnText }}</el-button></template>
                        </el-input>
                    </el-form-item>
                    <el-button v-loading="loading" type="primary" style="width: 100%;" @click.native.prevent="handleLogin">登 录</el-button>
                    <div class="type-check" @click="typeCheck">{{ type=='pwd'?'手机验证码登录':'账号密码登录' }}</div>
                </div>
            </el-form>
        </div>
        <!-- 首次登录修改密码 -->
        <el-dialog
            title="账号首次登录修改密码"
            width="500px"
            class="reset-pwd-dialog"
            :show-close="false"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            :visible.sync="dialogResetPwd"
        >
            <el-form ref="restPwdForm" label-position="right" label-width="80px" :model="restPwdForm" :rules="resetRules">
                <el-form-item prop="pwd" label="重置密码">
                    <el-input v-model="restPwdForm.pwd" type="password" placeholder="请输入密码" />
                </el-form-item>
                <el-form-item prop="pwd" label="确认密码">
                    <el-input v-model="restPwdForm.repwd" type="password" placeholder="请输入确认密码" />
                </el-form-item>
                <el-form-item style="text-align: right;">
                    <el-button type="primary" @click.native.prevent="handleRestPwd">提交</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
        <Copyright v-if="$store.state.settings.showCopyright" />
    </div>
</template>

<script>
import { sendCode, changeMy } from '@/api/common'
export default {
    name: 'Login',
    data() {
        return {
            title: process.env.VUE_APP_TITLE,
            type: 'pwd',
            form: {
                phone: localStorage.login_account || '',
                password: '',
                code: ''
            },
            restPwdForm: {
                realname: this.$store.state.user.realname,
                face: this.$store.state.user.avatar,
                accept: this.$store.state.user.accept,
                pwd: '',
                repwd: ''
            },
            rules: {
                phone: [
                    { required: true, trigger: 'change', message: '请输入手机号' }
                ],
                password: [
                    { required: true, trigger: 'blur', message: '请输入密码' },
                    { min: 6, max: 18, trigger: 'blur', message: '密码长度为6到18位' }
                ],
                code: [
                    { required: true, trigger: 'blur', message: '请输入验证码' }
                ]
            },
            loading: false,
            passwordType: 'password',
            btnText: '获取验证码',
            btnDisabled: false,
            redirect: undefined,
            dialogResetPwd: false,
            resetRules: {
                pwd: [
                    { required: true, trigger: 'blur', message: '请输入密码' },
                    { min: 6, max: 18, trigger: 'blur', message: '密码长度为6到18位' }
                ]
            }
        }
    },
    watch: {
        $route: {
            handler: function(route) {
                this.redirect = route.query && route.query.redirect
            },
            immediate: true
        }
    },
    mounted() {
        // var token = this.token
        var allParams = this.$route.query;
        var token = allParams.token || "";
        if (token) {
            this.$store.dispatch('user/checkMember', {token: token}).then(res => {
                if (res.need_change_pwd) {
                    this.restPwdForm.pwd = '';
                    this.restPwdForm.repwd = '';
                    this.dialogResetPwd = true;
                    this.loading = false;
                } else {
                    this.$store.dispatch('user/getPermissions', this.form).then(res => {
                        this.loading = false
                        this.$router.push({ path: this.redirect || '/' })
                    })
                    // this.loading = false
                    // this.$router.push({ path: this.redirect || '/' })
                }
            }).catch(() => {
                this.loading = false
            })
        }
    },
    methods: {
        typeCheck() {
            if (this.type == 'pwd') {
                this.type = 'phone'
            } else {
                this.type = 'pwd'
            }
            this.$refs.form.resetFields();
        },
        showPassword() {
            this.passwordType = this.passwordType === 'password' ? '' : 'password'
            this.$nextTick(() => {
                this.$refs.password.focus()
            })
        },
        getCode() {
            const phoneNumberPattern = /^1[3456789]\d{9}$/;
            if (phoneNumberPattern.test(this.form.phone)) {
                if (!this.btnDisabled) {
                    let m = 60;
                    sendCode({ phone: this.form.phone }).then(res => {
                        if (res.status) {
                            this.timer = setInterval(() => {
                                if (m > 0) {
                                    m--;
                                    this.btnText = `${m}s`;
                                    this.btnDisabled = true;
                                } else {
                                    this.btnText = `获取验证码`;
                                    this.btnDisabled = false;
                                    clearInterval(this.timer);
                                }
                            }, 1000)
                            this.$message({ type: 'success', message: '验证码已发送'});
                        }
                    })
                }
            } else {
                this.$message({ type: 'warning', message: '请输入正确的手机号码'});
            }
        },
        // 登录
        handleLogin() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.loading = true
                    if (this.type == 'pwd') {
                        this.$store.dispatch('user/login', this.form).then(res => {
                            if (res.need_change_pwd) {
                                this.restPwdForm.pwd = '';
                                this.restPwdForm.repwd = '';
                                this.dialogResetPwd = true;
                                this.loading = false;
                            } else {
                                this.$store.dispatch('user/getPermissions', this.form).then(res => {
                                    this.loading = false
                                    this.$router.push({ path: this.redirect || '/' })
                                })
                                // this.loading = false
                                // this.$router.push({ path: this.redirect || '/' })
                            }
                        }).catch(() => {
                            this.loading = false
                        })
                    } else {
                        this.$store.dispatch('user/smsLogin', this.form).then(res => {
                            if (res.need_change_pwd) {
                                this.restPwdForm.pwd = '';
                                this.restPwdForm.repwd = '';
                                this.dialogResetPwd = true;
                                this.loading = false;
                            } else {
                                this.$store.dispatch('user/getPermissions', this.form).then(res => {
                                    this.loading = false
                                    this.$router.push({ path: this.redirect || '/' })
                                })
                                // this.loading = false
                                // this.$router.push({ path: this.redirect || '/' })
                            }
                        }).catch(() => {
                            this.loading = false
                        })
                    }

                }
            })
        },
        // 重置密码
        handleRestPwd() {
            this.$refs.restPwdForm.validate(valid => {
                if (valid) {
                    if (this.restPwdForm.pwd != this.restPwdForm.repwd) {
                        this.$message.error('两次密码不一致！')
                        return;
                    }
                    changeMy(this.restPwdForm).then(res => {
                        if (res.status == '1') {
                            this.$message.success('设置成功，请重新登录！');
                            this.form.password = '';
                            this.dialogResetPwd = false;
                            this.type = 'pwd';
                        } else {
                            this.$message.error(res.message)
                        }
                    })
                }
            })

        }
    }
}
</script>

<style lang="scss" scoped>
[data-mode=mobile] {
    #login-box {
        max-width: 80%;
        .login-banner {
            display: none;
        }
    }
}
::v-deep .el-input {
    display: table !important;
}
::v-deep input[type=password]::-ms-reveal {
    display: none;
}
.bg-banner {
    position: absolute;
    z-index: 0;
    width: 100%;
    height: 100%;
    background-image: url(../assets/images/light-bg.png);
    background-size: 100% 100%;
    // background-image: url(../assets/images/logo.jpg);
    // background-size: 88px;
    // opacity: 0.1;
    // filter: blur(1px);
}
#login-box {
    display: flex;
    justify-content: space-between;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
    background: rgba(255, 255, 255, 1);
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 0 10px rgba($color: #000, $alpha: 0.1);
    .login-banner {
        width: 300px;
        background-image: url(../assets/images/logo.jpg);
        background-size: auto 100%;
        background-repeat: no-repeat;
        background-position: center center;
        margin: 40px 0;
    }
    .login-form {
        width: 350px;
        padding: 50px 35px 50px;
        overflow: hidden;
        .svg-icon {
            vertical-align: -0.35em;
        }
        .title-container {
            position: relative;
            .title {
                font-size: 22px;
                color: #666;
                margin: 0 auto 30px;
                text-align: center;
                font-weight: bold;
                @include text-overflow;
            }
        }
        .type-check {
            font-size: 14px;
            color: #409eff;
            margin-top: 10px;
            cursor: pointer;
        }
    }
    ::v-deep .el-input {
        display: inline-block;
        height: 48px;
        width: 100%;
        input {
            height: 48px;
        }
        .el-input__prefix {
            left: 10px;
        }
        .el-input__suffix {
            right: 10px;
        }
    }
    .el-checkbox {
        margin: 20px 0;
    }
}
.reset-pwd-dialog {
    ::v-deep .el-dialog__header {
        padding: 30px 20px 10px;
    }
    ::v-deep .el-dialog__body {
        padding: 20px 20px 10px;
    }
}
.copyright {
    position: absolute;
    bottom: 30px;
    width: 100%;
    margin: 0;
    mix-blend-mode: difference;
}
</style>
